package com.woniuxy.student.service.impl;

import com.woniuxy.commons.entity.PageBean;
import com.woniuxy.commons.entity.ReplySheet;
import com.woniuxy.commons.entity.TestPaper;
import com.woniuxy.commons.util.CommonUtil;
import com.woniuxy.student.dao.AnswerSheetDao;
import com.woniuxy.student.dao.TestPaperDao;
import com.woniuxy.student.service.TestPaperService;
import com.woniuxy.student.util.StudentCommonUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.text.ParseException;
import java.util.List;

/**
 * @Author: 曾令忠
 * @Description:
 * @Date: 2020/7/17 0017-下午 4:10
 */
@Service
public class TestPaperServiceImpl implements TestPaperService {
    @Resource
    TestPaperDao testPaperDao;
    @Resource
    AnswerSheetDao answerSheetDao;
    @Autowired
    RedisTemplate<String,Object> redisTemplate;
    /**
     * 分页查询试卷
     * @param userid
     * @param type
     * @param subject
     * @param page
     * @return
     */
    public PageBean<TestPaper> getPapersByCondition(Integer userid,String type, String subject, Integer page){
        PageBean<TestPaper> pb = new PageBean<>();
        int pagesize = 5;
        int totalNum = testPaperDao.getPapersNum(userid,type,subject);
        List<TestPaper> papers = testPaperDao.getPapersByCondition(userid,type,subject,(page - 1) * pagesize, pagesize);
        if (papers == null || papers.size() == 0) {
            return pb;
        }
        //判断作业或考试有没有过期
        for (TestPaper paper:papers) {
            try {
                paper.setPaperSubject(CommonUtil.getSubjectName(paper.getPaperSubject()));
                if(paper.getPaperType()<4){ //作业
                    paper.setExpired(CommonUtil.judgeTaskExpired(paper));
                }else{ //考试
                    int status = StudentCommonUtil.canStartTest(paper.getPaperDate(),paper.getPaperDuration());
                    if(status==3){
                        String key = "answer:"+paper.getPaperId()+":"+userid;
                        if (redisTemplate.hasKey(key)){
                            String res = (String)redisTemplate.opsForValue().get(key);
                            System.out.println(res+"res");
                            if ("-1".equals(res)){
                                status=2;//表示学生已提交
                            }else {
                                status=4;//表示学生已经开始考试了
                            }
                        }
                    }
                    paper.setPaperStatus(status);
                }
            } catch (ParseException e) {
                e.printStackTrace();
            }

        }
        pb.setList(papers);
        pb.setNowNum(papers.size());
        pb.setPageSize(pagesize);
        pb.setTotalNum(totalNum);
        pb.setTotalPage(totalNum % pagesize == 0 ? totalNum / pagesize : totalNum / pagesize + 1);
        pb.setNowPage(page);
        System.out.println(pb.getList());
        return pb;
    }

    @Override
    public TestPaper getTestPaperById(int paperid) {
        TestPaper paper = testPaperDao.getTestPaperById(paperid);
        return paper;
    }

    @Override
    public TestPaper getStudentTestPaper(int stuno, int paperid,TestPaper testPaper) {
        try{
            String key = "answer:"+paperid+":"+stuno;
            System.out.println("keyget:"+key);
            //看是否能获取到key对应的值，如果获取不到则证明是第一次，无需绑定答案
            Object code = redisTemplate.opsForValue().get(key);
            if (code==null){
                return testPaper;
            }
            String answers = (String) code;
            System.out.println("answers:"+answers);
            if("".equals(answers)){
                return testPaper;
            }
            List<ReplySheet> sheets = StudentCommonUtil.parseAnswersArray(answers);
            testPaper = StudentCommonUtil.setValue(testPaper,sheets);
            System.out.println("testPaper："+testPaper.getPaperQuestions().get(0).getPaperDetailQuestion());
        }catch (Exception e){
           e.printStackTrace();
        }
        return testPaper;
    }


}
